'use strict';
//1.正确使用egg搭建后台服务（10分）
const Controller = require('egg').Controller;
const updataRule = {
  comName: 'string',
  class: "string",
  timer: "string",
  classify: "string",
  token:"string",
  imgurl:"string",
}
const idRule = {
  id: "string",
}
class museController extends Controller {
  async update() {
    const { ctx } = this;
    ctx.validate({ ...updataRule, ...idRule }, { ...ctx.request.body, ...ctx.params });
    const updatas = await ctx.service.muse.getUpdata({ ...ctx.request.body, ...ctx.params });
    if (updatas.affectedRows == 1) {
      ctx.body = {
        code: 2,
        msg: "修改成功"
      }
    } else {
      ctx.body = {
        code: 4,
        msg: "修改失败"
      }
    }
  }
  async create() {
      const {ctx} = this;
      ctx.validate(updataRule, ctx.request.body)
      const addname = await ctx.service.muse.create(ctx.request.body);
      if(addname.affectedRows==1){
          ctx.body = {
            code:2,
            msg:"添加成功",
          }
      }else{
          ctx.body = {
            code:4,
            msg:"添加失败",
          }
      }
  }
  async destroy(){
    const {ctx} = this;
    ctx.validate(idRule, ctx.params);
    const del = await ctx.service.muse.destroy(ctx.params)
    if(del.affectedRows==1){
      ctx.body = {
        code:2,
        msg:"删除成功",
      }
    }else{
      ctx.body = {
        code:4,
        msg:"删除失败"
      }
    }
  }
  async index(){
    const {ctx} = this;
    const {page=1,pagesize=6,search=""} = ctx.query;
    try {
      const del = await ctx.service.muse.index(page,pagesize,search);
      ctx.body = {
        code:2,
        msg:"获取成功",
        ...del,
      }
    } catch (error) {
      ctx.body = {
        code:4,
        msg:"获取失败",
        error
      }
    }
  }
}

module.exports = museController;
